Page({
    data:{
        cartData:[],//购物车数据
        cartObj:{ //当前购物车选中待支付数据信息
            number:0,
            price:"0.00"
        },
        allChecked:false, //是否是全选状态
        isUpdating:false, //是否在编辑状态
    },
    onLoad(){
        // this.getCartObj();
    },
    async onShow(){
        await this.getCartObj();
        // console.log(this.selectAllComponents(".crab-area"))
        // console.log(this.selectComponent(".crab-area"))
        this.selectAllComponents(".crab-area").forEach((item)=>{
            item.update();
        });
    },
    // 点击编辑文字事件监听
    updateData(){
        let isUpdating=!this.data.isUpdating;
        this.setData({
            isUpdating
        });
    },
    // 自定义组件点击加购按钮事件
    addCart(e:any){
        let carts=wx.getStorageSync("carts") || [];
        carts.forEach((item:any)=>{
            if(item.id === e.detail.id){
                item.number=item.number+1;
                item.checked=true;
            };
        });
        wx.setStorageSync("carts",carts);
        this.getCartObj();
    },
    // 自定义组件点击减购图标事件监听
    minusCart(e:any){
        let carts=wx.getStorageSync("carts") || [];
        carts.forEach((item:any)=>{
            if(item.id === e.detail.id && item.number !==0){
                item.number=item.number-1;
            };
        });
        carts=carts.filter((item:any)=>{
            return item.number !==0;
        });
        wx.setStorageSync("carts",carts);
        this.getCartObj();
    },
    // 点击选中图标事件监听
    checkData(e:any){
        // console.log(e)
        let carts=wx.getStorageSync("carts") || [];
        carts.forEach((item:any)=>{
            if(e.currentTarget.dataset.id === item.id){
                item.checked=!item.checked;
            };
        });
        wx.setStorageSync("carts",carts);
        this.getCartObj();
    },
    // 点击全选图标事件监听
    allCheckedData(){
        let allChecked=!this.data.allChecked;
        let carts=wx.getStorageSync("carts") || [];
        carts.forEach((item:any)=>{
            if(allChecked){
                item.checked=true;
            }else{
                item.checked=false;
            };
        });
        wx.setStorageSync("carts",carts);
        this.getCartObj();
        this.setData({
            allChecked
        });
    },
    // 点击删除按钮事件监听
    deleteData(){
        let carts=wx.getStorageSync("carts") || [];
        carts.forEach((item:any)=>{
            if(item.checked){
                item.number=0;
            };
        });
        carts=carts.filter((item:any)=>{
            return item.number !==0;
        });
        wx.setStorageSync("carts",carts);
        this.getCartObj();
    },
    async getCartObj(){
        //初始化获取购物车中数据
        let carts=wx.getStorageSync("carts") || [];
        let number=0,price=0;
        carts.forEach((item:any)=>{
            if(item.checked){ //对选中的数据进行计算
                number+=item.number;
                price+=(item.number*item.price);
            };
        });
        if(carts.every((item:any)=>{return item.checked})){ //若每一项的checked都为true，则全选
            this.data.allChecked=true;
        }else{
            this.data.allChecked=false;
        };
        this.data.cartObj.number=number;
        this.data.cartObj.price=price+".00";
        this.setData({
            cartData:carts,
            cartObj:this.data.cartObj,
            allChecked:this.data.allChecked
        });
    }
});